package com.zmx.common.common.invocation;

import com.google.common.collect.Maps;
import com.zmx.common.common.security.dto.OauthTokenDto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 线程参数
 * @ClassName: InvocationInfo
 * @Author zhaomxr
 * @Date 2021-08-24 16:11
 */
@Data
@Slf4j
@AllArgsConstructor
public class InvocationInfo {

    private String userId;

    private String userName;

    private String systemId;

    private String threadName;

    private String tocken;

    private Map parametersMap;

    private Map extendAttributes;

    private OauthTokenDto oauthTokenDto;

    private String sys_code;

    public InvocationInfo() {
    }

    public InvocationInfo(String cookies) {
        this.set(cookies);
    }

    /**
     * 设置当前线程信息
     */
    public void set(String cookies) {
        String[] cookieList = cookies.split("; ");
        HashMap cookieMap = Maps.newHashMapWithExpectedSize(cookieList.length);

        Arrays.stream(cookieList).forEach(s -> {
            String[] split = s.split("=");
            if (!cookieMap.containsKey(split[0])) {
                cookieMap.put(split[0].trim(), split[1].trim());
            }
        });

        this.userId = cookieMap.get("userId") == null ? "" : String.valueOf(cookieMap.get("userId"));
        this.userName = cookieMap.get("userName") == null ? "" : String.valueOf(cookieMap.get("userName"));
        this.systemId = cookieMap.get("systemId") == null ? "" : String.valueOf(cookieMap.get("systemId"));
        this.threadName = cookieMap.get("threadName") == null ? "" : String.valueOf(cookieMap.get("threadName"));
        this.tocken = cookieMap.get("tocken") == null ? "" : String.valueOf(cookieMap.get("tocken"));
    }

}
